package com.labichaoka.chaoka.ui.baseinfo.hand;

import com.labichaoka.chaoka.base.MyApplication;
import com.labichaoka.chaoka.entity.BaseResponse;
import com.labichaoka.chaoka.entity.IDCardRequest;
import com.labichaoka.chaoka.entity.IDCardResponse;
import com.labichaoka.chaoka.entity.RealNameRequest;
import com.labichaoka.chaoka.net.BaseSubscriber;
import com.labichaoka.chaoka.net.DataManager;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by ruan on 2018/8/8.
 */

public class HandImgInteractorImpl implements HandImgInteractor{
    private DataManager manager = new DataManager(MyApplication.mContext);
    private CompositeSubscription mCompositeSubscription = new CompositeSubscription();

    @Override
    public void uploadFile(IDCardRequest request, OnUploadFinishedListener listener) {
        mCompositeSubscription.add(manager.uploadFile(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onUploadFailed();
                    }

                    @Override
                    public void onNext(BaseResponse response) {
                        super.onNext(response);
                        listener.onUploadSuccessed(response);
                    }
                })
        );
    }

    @Override
    public void onQueryIDCard(OnUploadFinishedListener listener) {
        mCompositeSubscription.add(manager.queryIDCard()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<IDCardResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onQueryIDCardFailed();
                    }

                    @Override
                    public void onNext(IDCardResponse response) {
                        super.onNext(response);
                        listener.onQueryIDCardSuccessed(response);
                    }
                })
        );
    }

    @Override
    public void resetRealName(RealNameRequest realNameRequest, OnUploadFinishedListener listener) {
        mCompositeSubscription.add(manager.resetRealName(realNameRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onResetRealNameFailed();
                    }

                    @Override
                    public void onNext(BaseResponse response) {
                        super.onNext(response);
                        listener.onResetRealNameSuccessed(response);
                    }
                })
        );
    }

    @Override
    public void resetCardId(OnUploadFinishedListener listener) {
        mCompositeSubscription.add(manager.resetCardId()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onResetCardIdFailed();
                    }

                    @Override
                    public void onNext(BaseResponse response) {
                        super.onNext(response);
                        listener.onResetCardIdSucc(response);
                    }
                })
        );
    }
}
